using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace ExchangeHog.Pop3Client.Responses
{
	internal class Pop3Response
	{
		internal byte[] ResponseContents { get; private set; }
		public bool StatusIndicator { get; private set; }
		public string HostMessage { get; private set; }

		public Pop3Response(byte[] responseContents, string hostMessage, bool statusIndicator)
		{
			if (responseContents == null)
				throw new ArgumentNullException("responseBuffer");

			if (string.IsNullOrEmpty(hostMessage))
				throw new ArgumentNullException("hostMessage");

			ResponseContents = responseContents;
			HostMessage = hostMessage;
			StatusIndicator = statusIndicator;
		}

		public static Pop3Response CreateResponse(byte[] responseContents)
		{
			using (StreamReader reader = new StreamReader(new MemoryStream(responseContents)))
			{
				string hostMessage = reader.ReadLine();
				if (hostMessage == null)
					return null;

				return new Pop3Response(responseContents, hostMessage, hostMessage.StartsWith(Pop3Responses.Ok));
			}
		}
	}
}